/**
 * Created:	2001-7-
 * By:		eeorange
 * About: 	uva 10098 - Generating Fast, Sorted Permutation
 * Status:	AC 464ms
 */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAXN		10+1

void print_permutation(int n, char* P, char* A, int cur){
	if(cur == n){
		for(int i=0; i<n; i++) printf("%c", A[i]);
		printf("\n");
	}
	else for(int i=0; i<n; i++) if(!i || P[i] != P[i-1])	// permutation all
	{
		int v = P[i];
		int c1 = 0, c2 = 0;
		for(int j=0; j<cur; j++) if(A[j] == v) c1++;
		for(int j=0; j<n; j++) if(P[j] == v) c2++;
		if(c1 < c2){
			A[cur] = v;
			print_permutation(n, P, A, cur+1);
		}
	}
}

int main(){
	char P[MAXN], A[MAXN];
	int nr_cases;
	scanf("%d", &nr_cases);
	while(nr_cases--)
	{
		scanf("%s", P);
		int n = strlen(P);
		sort(P, P+n);
		print_permutation(n, P, A, 0);
		printf("\n");
	}
	
	return 0;	
}


